package com.zfx.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zfx.pojo.Admin;
import com.zfx.service.IAdminService;
import com.zfx.util.JsonMsg;
import com.zfx.util.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * @description:
 * @author: zfx
 * @version: 1.0
 * @time: 2020-05-08 0:12
 * <p>
 * 修改历史：
 * 修改日期      修改人员    版本    内容
 * 2020-05-08   zfx       1.0
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    @RequestMapping("/login")
    public JsonObject login(HttpServletRequest request, @RequestBody Admin admin) {
        JsonObject jsonObject = new JsonObject();

        Admin login = adminService.login(admin);

        if(login != null) {
            request.getSession().setAttribute("loginId", login.getLoginId());
            jsonObject.setCode("200");
            jsonObject.setMsg("success");
            jsonObject.setData("str", login.getPermission()+"_"+login.getId()+"_"+login.getUserName());
        } else {
            jsonObject.setCode("500");
            jsonObject.setMsg("error");
        }
        return jsonObject;
    }

    @RequestMapping("/getInfo")
    public JsonObject getInfo(@RequestParam(name="id") int id) {
        JsonObject jsonObject = new JsonObject();
        Admin admin = adminService.findById(id);
        if(admin != null) {
            jsonObject.setCode("200");
            jsonObject.setMsg("success");
            jsonObject.setData("admin", admin);
        } else {
            jsonObject.setCode("500");
            jsonObject.setMsg("error");
        }
        return jsonObject;
    }

    @RequestMapping("/update")
    public JsonObject update(@RequestBody Admin admin) {
        JsonObject jsonObject = new JsonObject();
        if(adminService.update(admin) == 1) {
            jsonObject.setCode("200");
            jsonObject.setMsg("success");
        } else {
            jsonObject.setCode("500");
            jsonObject.setMsg("error");
        }
        return jsonObject;
    }

    @RequestMapping("/changePassword")
    public JsonObject changePassword(@RequestBody String jsonData) throws IOException {
        JsonObject jsonObject = new JsonObject();
        ObjectMapper objectMapper = new ObjectMapper();

        HashMap<String, String> jsonMap = objectMapper.readValue(jsonData, HashMap.class);
        String id = jsonMap.get("id");
        String newpwd = jsonMap.get("newpwd");
        String oldpwd = jsonMap.get("oldpwd");

        Admin admin = adminService.findById(Integer.valueOf(id));
        if(!oldpwd.equals(admin.getPassword())) {
            jsonObject.setCode("500");
            jsonObject.setMsg("error");
        } else {
            admin.setPassword(newpwd);
            if(adminService.update(admin) == 1) {
                jsonObject.setCode("200");
                jsonObject.setMsg("success");
            } else {
                jsonObject.setCode("500");
                jsonObject.setMsg("error");
            }
        }
        return jsonObject;
    }

    @RequestMapping("/list")
    public JsonObject getList(@RequestBody String jsonData) throws IOException {
        JsonObject jsonObject = new JsonObject();
        ObjectMapper objectMapper = new ObjectMapper();

        HashMap<String, Object> jsonMap = objectMapper.readValue(jsonData, HashMap.class);
        Admin admin = new Admin(jsonMap);

        List<Admin> adminList = adminService.findByAdmin(admin, (int) jsonMap.get("page"), (int)jsonMap.get("limit"));
        jsonObject.setData("adminList", adminList);
        jsonObject.setDataSize(adminService.countAll(admin));
        jsonObject.setCode("200");
        jsonObject.setMsg("success");

        return jsonObject;
    }

    @RequestMapping("/save")
    public JsonObject save(@RequestBody Admin admin) {
        JsonObject jsonObject = new JsonObject();

        int saveNum = adminService.save(admin);

        if(saveNum == 1) {
            jsonObject.setCode("200");
            jsonObject.setMsg("success");
        } else if(saveNum == -1) {
            jsonObject.setCode("500");
            jsonObject.setMsg("error1");
        } else {
            jsonObject.setCode("500");
            jsonObject.setMsg("error");
        }
        return jsonObject;
    }

    @RequestMapping("/deleteOne")
    public JsonObject deleteOne(@RequestParam(name = "id") int id) {
        JsonObject jsonObject = new JsonObject();
        int deleteNum = adminService.deleteById(id);
        if(deleteNum == 1) {
            jsonObject.setCode("200");
            jsonObject.setMsg("success");
        } else {
            jsonObject.setCode("500");
            jsonObject.setMsg("error");
        }
        return jsonObject;
    }

    @RequestMapping("/findById")
    public JsonObject findById(@RequestParam(name = "id") int id) {
        JsonObject jsonObject = new JsonObject();
        Admin admin = adminService.findById(id);
        if(admin != null) {
            jsonObject.setCode("200");
            jsonObject.setMsg("success");
            jsonObject.setData("admin", admin);
        } else {
            jsonObject.setCode("500");
            jsonObject.setMsg("error");
        }
        return jsonObject;
    }

}
